# 文件说明：定义计算器模型，处理计算逻辑

import math
import re

class CalculatorModel:
    def __init__(self):
        self.history = []

    def calculate(self, expression):
        try:
            # 正则表达式匹配有效的数学表达式
            pattern = re.compile(r'^[\d+\-*/().eπsinlcosqrtlogpow ]+$')
            if not pattern.match(expression):
                raise ValueError("Invalid expression")

            # 替换数学常数
            expression = expression.replace('pi', 'math.pi')
            expression = expression.replace('e', 'math.e')

            # 计算结果
            result = eval(expression, {"__builtins__": None}, {"math": math})
            self.history.append(f"{expression} = {result}")
            return result
        except Exception as e:
            return f"Error: {str(e)}"

    def get_history(self):
        return self.histor